/*
Identify births in order to connect them to parents.

Uses:
data/admin/raw/ancestry_as_of_2021.dta

Creates:
data/admin/intermediate/birthyears_by_parent.dta
data/admin/intermediate/first_births_to_2019.dta
*/

cd "$root"

* start with all births from 1900-2020
* link parents to years in which they had a new child
use data/admin/raw/ancestry_as_of_2021.dta, clear
keep b_year mor_lnr far_lnr
destring b_year, replace
ren (mor_lnr far_lnr) (lnr_m lnr_f)

gen temp_id = _n
reshape long lnr, i(temp_id) j(id) string
drop temp_id id
drop if lnr == .

* this is now lnr * year
* now can identify year of first birth by taking the minimum year

keep if b_year <= 2019
collapse (min) year_first_child = b_year ///
         (max) year_last_child = b_year, by(lnr)
save data/admin/intermediate/first_births_to_2019.dta, replace
